package com.company.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.company.project.common.config.FileUploadProperties;
import com.company.project.entity.SysProjectStepEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.List;
import com.company.project.common.utils.DataResult;

import com.company.project.entity.SysProjectStepFileEntity;
import com.company.project.service.SysProjectStepFileService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;


/**
 * 项目步骤附件表
 *
 * @author wenbin
 * @email *****@mail.com
 * @date 2022-03-04 10:33:02
 */
@Controller
@RequestMapping("/")
public class SysProjectStepFileController {
    @Resource
    private FileUploadProperties fileUploadProperties;

    @Autowired
    private SysProjectStepFileService sysProjectStepFileService;


    /**
    * 跳转到页面
    */
    @GetMapping("/index/sysProjectStepFile/{stepId}")
    public String sysProjectStepFile(ModelMap model,@PathVariable  String stepId) {
        model.put("stepId",stepId);
        return "sysprojectstepfile/list";
        }

    @ApiOperation(value = "新增")
    @PostMapping("sysProjectStepFile/upload")
    @RequiresPermissions("sysProjectStepFile:add")
    @ResponseBody
    public DataResult add(@RequestParam(value = "file") MultipartFile file,@RequestParam String stepId) {
        //判断文件是否空
        if (file == null || file.getOriginalFilename() == null || "".equalsIgnoreCase(file.getOriginalFilename().trim())) {
            return DataResult.fail("文件为空");
        }
        return sysProjectStepFileService.saveFile(file,stepId);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("sysProjectStepFile/delete")
    @RequiresPermissions("sysProjectStepFile:delete")
    @ResponseBody
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids){
        sysProjectStepFileService.removeByIds(ids);
        return DataResult.success();
    }

    @ApiOperation(value = "更新")
    @PutMapping("sysProjectStepFile/update")
    @RequiresPermissions("sysProjectStepFile:update")
    @ResponseBody
    public DataResult update(@RequestBody SysProjectStepFileEntity sysProjectStepFile){
        sysProjectStepFileService.updateById(sysProjectStepFile);
        return DataResult.success();
    }

    @ApiOperation(value = "查询分页数据")
    @PostMapping("sysProjectStepFile/listByPage")
    @RequiresPermissions("sysProjectStepFile:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody SysProjectStepFileEntity sysProjectStepFile){
        Page page = new Page(sysProjectStepFile.getPage(), sysProjectStepFile.getLimit());
        LambdaQueryWrapper<SysProjectStepFileEntity> queryWrapper = Wrappers.lambdaQuery();
        //必须传入步骤主键，否则无数据
        if(!StringUtils.isEmpty(sysProjectStepFile.getStepId())){
            queryWrapper.like(SysProjectStepFileEntity::getStepId, sysProjectStepFile.getStepId());
        }else{
            queryWrapper.like(SysProjectStepFileEntity::getDeleted, "0");
        }
        //查询文件名称
        if(!StringUtils.isEmpty(sysProjectStepFile.getFileName())){
            queryWrapper.like(SysProjectStepFileEntity::getFileName, sysProjectStepFile.getFileName());
        }
        queryWrapper.orderByAsc(SysProjectStepFileEntity::getCreateTime);
        List<SysProjectStepFileEntity> iPage = sysProjectStepFileService.list(queryWrapper);
        iPage.forEach(item->item.setFilePath(fileUploadProperties.getUrl() + item.getFilePath()));
        return DataResult.success(iPage);
    }

}
